package com.yanqu.road.dao.impl.activity.sonhai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiActivityData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiBeastGroupData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiEggData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPatronsData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SonHaiEggDataDaoImpl extends TempDataObjectDao<SonHaiEggData> {

    public static String getSonHaiEggDataSql = "select * from `t_c_activity_son_hai_egg_data` where `activityId`=? and groupId = ?";

    public Map<Integer, SonHaiEggData> getSonHaiEggData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SonHaiEggData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSonHaiEggDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                SonHaiEggData data = parse(rs);
                map.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSonHaiEggDataSql = "update `t_c_activity_son_hai_egg_data` set eggConfigId=?,eggName=?,findUserId=?,beastGroupData=?,userId=?,patronsDataMap=?,completeTime=?,occupyTime=? where activityId = ? and groupId = ? and id = ?";

    @Override
    protected boolean updateData(SonHaiEggData sonHaiEggData) {
        sonHaiEggData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getEggConfigId()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiEggData.getEggName()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getFindUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiEggData.getBeastGroupData())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiEggData.getPatronsDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getCompleteTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getOccupyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getId()));
        if (getDb().executeNoneQuery(updateSonHaiEggDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiEggDataSql = "insert into `t_c_activity_son_hai_egg_data`(activityId,groupId,id,eggConfigId,eggName,findUserId,beastGroupData,userId,patronsDataMap,completeTime,occupyTime) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(SonHaiEggData sonHaiEggData) {
        sonHaiEggData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiEggData.getEggConfigId()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiEggData.getEggName()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getFindUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiEggData.getBeastGroupData())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiEggData.getPatronsDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getCompleteTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiEggData.getOccupyTime()));
        if (getDb().executeNoneQuery(insertSonHaiEggDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected SonHaiEggData parseData(ResultSet rs) throws SQLException {
        SonHaiEggData sonHaiEggData = new SonHaiEggData();
        sonHaiEggData.setActivityId(rs.getInt("activityId"));
        sonHaiEggData.setGroupId(rs.getInt("groupId"));
        sonHaiEggData.setId(rs.getInt("id"));
        sonHaiEggData.setEggConfigId(rs.getInt("eggConfigId"));
        sonHaiEggData.setFindUserId(rs.getLong("findUserId"));
        sonHaiEggData.setBeastGroupData(JSON.parseObject(rs.getString("beastGroupData"), SonHaiBeastGroupData.class));
        sonHaiEggData.setUserId(rs.getLong("userId"));
        sonHaiEggData.setPatronsDataMap(JSON.parseObject(rs.getString("patronsDataMap"), new TypeReference<Map<Integer, SonHaiPatronsData>>() {
        }));
        sonHaiEggData.setCompleteTime(rs.getLong("completeTime"));
        sonHaiEggData.setEggName(rs.getString("eggName"));
        sonHaiEggData.setOccupyTime(rs.getLong("occupyTime"));
        return sonHaiEggData;
    }
}